local HurtStrategy = require("gameLogic.fight.fightHurtStrategy.HurtStrategy")
local HurtElement = class("HurtElement", HurtStrategy)
local Hurt = require("gameLogic.fight.fightHurtStrategy.Hurt")
local FightConst = require("const.FightConst")
local AttributeConst = require("const.AttributeConst")
local AttrDataHelper = require("data.helper.AttrDataHelper")

function HurtElement:ctor(config, skill, element)
    self._element = element
    HurtElement.super.ctor(self, config, skill)
end

function HurtElement:hurt(target)
    local ratio = self._config.ratio
    local entity = self._skill:getEntity()
    local value = (entity:getAttrValue(AttributeConst.ATTACK) * ratio)
    local defense = target:getAttrValue(AttributeConst.DEFENSE)
    local hurt = value - (defense / (defense + FightConst.DEFENSE_FIX_RATIO) * value) -- 防御
    hurt = hurt < 0 and 1 or hurt

    local finalHurt, isCrit = self:_critHurt(hurt)
    local elementHurt = AttributeConst.ELEMENT_HURT_MAP[self._element]
    elementHurt = entity:getAttrValue(elementHurt)
    local hurtAdd = entity:getAttrValue(AttributeConst.HURT_ADD)

    local feature = entity:getFeature()
    local raceAdd = entity:getAttrValue(AttrDataHelper.getRaceHurtAddAttrId(feature.race))
    local sexAdd = entity:getAttrValue(AttrDataHelper.getSexHurtAttrId(feature.sex))

    local myElement = self._element
    local tarElement = entity:getElement()
    local restrainAdd = AttributeConst.ELEMENT_RESTRAINT[myElement .. "_" .. tarElement] or 0
    restrainAdd = restrainAdd * 1000
    
    finalHurt = finalHurt + (finalHurt * ((elementHurt + hurtAdd + raceAdd + sexAdd + restrainAdd) / 1000))

    local resist = AttributeConst.ELEMENT_RESIST_MAP[element]
    local resist = target:getAttrValue(resist)
    local allResist = target:getAttrValue(AttributeConst.HURT_RESIST)
    finalHurt = finalHurt - (finalHurt * ((resist + allResist) / 1000)) -- 抗性千分比

    finalHurt = finalHurt < 0 and 1 or finalHurt
    finalHurt = math.floor(finalHurt)
    local hurtObj = Hurt.new(finalHurt, isCrit, self._element, self._skill:canCounterattack(), entity)

    return hurtObj
end

return HurtElement
